//
//  MoodEntryCell.swift
//  gamlify
//
//  Created by Admin on 2024/12/8.
//

import UIKit
import SnapKit

class GamAICell: UITableViewCell {
    // MARK: - Properties
    // 日期
    private lazy var dateLabel: UILabel = {
        let label = UILabel()
        label.font = .systemFont(ofSize: 12)
        label.textColor = UIColor.hex(0xA7B6CC)
        label.text = "Today"
        return label
    }()
    
    private lazy var cardView: UIView = {
        let view = UIView()
        view.layer.borderWidth = 1
        view.layer.borderColor = UIColor.hex(0xE9EEF7).cgColor
        view.layer.cornerRadius = 4
        view.backgroundColor = .white
        return view
    }()
    
    // 进度
    lazy var progressView: IndexProgressView = {
        let view = IndexProgressView(frame: .zero)
        view.progress = 1
        return view
    }()
    
    lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.hex(0xF5F7F9)
        return view
    }()
    
    private let timeLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 10)
        label.backgroundColor = UIColor.hex(0xF5F7F9)
        label.layer.cornerRadius = 2
        label.textColor = UIColor.hex(0x899AB2)
        label.text = "12.01"
        return label
    }()
    
    private lazy var arrowImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "arrow_right")
        return imageView
    }()
    
    private let entryLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 16)
        label.numberOfLines = 3
        return label
    }()
    
    private let entryImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFill
        imageView.clipsToBounds = true
        imageView.layer.cornerRadius = 4
        imageView.image = UIImage(named: "placeholder_image")
        return imageView
    }()
    
    private let tagStackView: UIStackView = {
        let stackView = UIStackView()
        stackView.axis = .horizontal
        stackView.spacing = 8
        return stackView
    }()
    
    // MARK: - Initialization
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - Setup
    private func setupViews() {
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        contentView.addSubview(dateLabel)
        contentView.addSubview(cardView)
        
        cardView.addSubview(progressView)
        cardView.addSubview(timeLabel)
        cardView.addSubview(arrowImageView)
        cardView.addSubview(entryLabel)
        cardView.addSubview(entryImageView)
        cardView.addSubview(lineView)
        cardView.addSubview(tagStackView)
        
        dateLabel.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.left.equalToSuperview().offset(15)
            make.height.equalTo(15)
        }
        
        cardView.snp.makeConstraints { make in
            make.top.equalTo(dateLabel.snp.bottom).offset(4)
            make.left.equalToSuperview().offset(15)
            make.right.equalToSuperview().offset(-15)
            make.bottom.equalToSuperview().offset(-16)
        }
        
        progressView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(8)
            make.left.equalToSuperview().offset(8)
            make.width.equalTo(144)
            make.height.equalTo(26)
        }
        
        timeLabel.snp.makeConstraints { make in
            make.centerY.equalTo(progressView)
            make.right.equalToSuperview().offset(-30)
            make.height.equalTo(14)
            make.width.lessThanOrEqualTo(40)
        }
        
        arrowImageView.snp.makeConstraints { make in
            make.centerY.equalTo(progressView)
            make.right.equalToSuperview().offset(-8)
            make.width.height.equalTo(12)
        }
        
        entryLabel.snp.makeConstraints { make in
            make.top.equalTo(progressView.snp.bottom).offset(6)
            make.leading.equalTo(progressView)
            make.trailing.equalTo(entryImageView.snp.leading).offset(-43)
        }
        
        entryImageView.snp.makeConstraints { make in
            make.top.equalTo(entryLabel)
            make.trailing.equalToSuperview().offset(-8)
            make.height.width.equalTo(64)
        }
        
        lineView.snp.makeConstraints { make in
            make.top.equalTo(entryLabel.snp.bottom).offset(6)
            make.leading.equalToSuperview().offset(8)
            make.trailing.equalToSuperview().offset(-8)
            make.height.equalTo(1)
        }
        
        tagStackView.snp.makeConstraints { make in
            make.top.equalTo(lineView.snp.bottom).offset(6)
            make.leading.equalToSuperview().offset(9)
            make.trailing.lessThanOrEqualToSuperview().offset(-8)
            make.bottom.equalToSuperview().offset(-8)
        }
    }
    
    // MARK: - Configuration
    func configure(with mood: String, time: String, entry: String, image: UIImage?, tags: [String]) {
//        timeLabel.text = time
        entryLabel.text = entry
//        entryImageView.image = image
//        entryImageView.isHidden = image == nil
        
        // Clear existing tags
        tagStackView.arrangedSubviews.forEach { $0.removeFromSuperview() }
        
        // Add new tags
        for tag in tags {
            let tagView = createTagView(with: tag)
            tagStackView.addArrangedSubview(tagView)
        }
    }
    
    private func createTagView(with title: String) -> UIView {
        let tagView = UIView()
        tagView.backgroundColor = .systemBlue.withAlphaComponent(0.1)
        tagView.layer.cornerRadius = 12
        
        let tagLabel = UILabel()
        tagLabel.text = title
        tagLabel.font = UIFont.systemFont(ofSize: 12, weight: .medium)
        tagLabel.textColor = .systemBlue
        
        tagView.addSubview(tagLabel)
        
        tagLabel.snp.makeConstraints { make in
            make.edges.equalToSuperview().inset(UIEdgeInsets(top: 4, left: 8, bottom: 4, right: 8))
        }
        
        return tagView
    }
}

